package com.yuju.controller.group;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.EmployeeReturnCodeEnum;
import com.yuju.group.model.EmployeeModel;
import com.yuju.group.service.IEmployeeService;
import com.yuju.model.TreeModel;
import com.yuju.util.Utils;
import com.yuju.vo.group.EmployeeVO;
/**
 * 
 * 类名：EmployeeController  <br />
 *
 * 功能：员工相关
 *
 * @author : zhousc <br />
 * @Date : 2017年11月24日 上午9:45:40  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("employee")
public class EmployeeController extends BaseController<EmployeeVO, EmployeeModel> {

	@Autowired
	private IEmployeeService employeeService;
	
	/**
	 * 
	 * 功能：<br/> 员工信息列表
	 *
	 * @author zhousc
	 * @Date 2017年11月24日 下午2:05:13 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj)   {
		EmployeeVO employeeVO = obj.toJavaObject(EmployeeVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(employeeVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		if(Utils.isNotNull(employeeVO.getQ())){
			employeeVO.setMyLike_name(employeeVO.getQ());
		}
		Result result = new Result();
		PageModel pageModel = employeeService.pageList(convert(employeeVO), employeeVO.getPage(), employeeVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>员工添加
	 *
	 * @author zhousc
	 * @Date 2017年11月24日 下午2:30:05 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj)   {
		EmployeeVO employeeVO = obj.toJavaObject(EmployeeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(employeeVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = employeeService.save(convert(employeeVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_SAVE_SUCCESS.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_SAVE_FAILURE.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>员工更新
	 *
	 * @author zhousc
	 * @Date 2017年11月24日 下午2:30:26 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj)   {
		EmployeeVO employeeVO = obj.toJavaObject(EmployeeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(employeeVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = employeeService.updateSelective(convert(employeeVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_UPDATE_FAILURE.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	/**
	 * 
	 * 功能：<br/>员工删除
	 *
	 * @author zhousc
	 * @Date 2017年11月24日 下午2:32:02 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj)   {
		EmployeeVO employeeVO = obj.toJavaObject(EmployeeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(employeeVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = employeeService.del(employeeVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_DEL_SUCCESS.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_DEL_FAILURE.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>根据ID查询员工信息
	 *
	 * @author zhousc
	 * @Date 2017年11月24日 下午2:34:12 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj)   {
		EmployeeVO employeeVO = obj.toJavaObject(EmployeeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(employeeVO, "get", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		EmployeeModel employeeModel = employeeService.findById(employeeVO.getId());
		if (Utils.isNotNull(employeeModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(employeeModel);
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：<br/>根据集团ID获取本集团最新的员工编号，回显到添加页面
	 *
	 * @author zhouzhou
	 * @Date 2017年11月24日 下午3:11:16 <br/>
	 */
//	@RequestMapping("getEmpCode")
//	@ResponseBody
//	public Result getEmpCode(@RequestBody JSONObject obj)   {
//		EmployeeVO employeeVO = obj.toJavaObject(EmployeeVO.class);
//		
//		Result result = new Result();
//		String empCode = employeeService.getEmpCode(employeeVO.getGroupId());
//		if(Utils.isNotNull(empCode)){
//			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
//			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
//			result.setData(empCode);
//			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_GETEMPCODE_SUCCESS.getCode());
//			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_GETEMPCODE_SUCCESS.getDesc());
//		} else {
//			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
//			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
//			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_GETEMPCODE_FAILURE.getCode());
//			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_GETEMPCODE_FAILURE.getDesc());
//		}
//		return result;
//	}
	/**
	 * 
	 * 功能：<br/>编辑页面部门树展示
	 *
	 * @author zhouzhou
	 * @Date 2017年11月24日 下午4:15:58 <br/>
	 */
	@RequestMapping("getDepartmentTree")
	@ResponseBody
	public Result getDepartmentTree(@RequestBody JSONObject obj)   {
		EmployeeVO employeeVO = obj.toJavaObject(EmployeeVO.class);
		Result result = new Result();
		List<TreeModel> departmentTree = employeeService.getDepartmentTree(employeeVO.getGroupId());
		if(Utils.isNotNull(departmentTree)){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(departmentTree);
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_DEPARTMENTTREE_SUCCESS.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_DEPARTMENTTREE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_DEPARTMENTTREE_FAILURE.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_DEPARTMENTTREE_FAILURE.getDesc());
		}
		return result;
	}
	@RequestMapping("getEmpInfo")
	@ResponseBody
	public Result getEmpInfo(@RequestBody JSONObject obj)   {
		EmployeeVO employeeVO = obj.toJavaObject(EmployeeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(employeeVO, "getEmpInfo", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		EmployeeModel employeeModel  = employeeService.getEmpInfo(employeeVO.getOpId());
		if(Utils.isNotNull(employeeModel)){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(employeeModel);
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_DEPARTMENTTREE_SUCCESS.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_DEPARTMENTTREE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(EmployeeReturnCodeEnum.EMPLOYEE_DEPARTMENTTREE_FAILURE.getCode());
			result.setReturnMsg(EmployeeReturnCodeEnum.EMPLOYEE_DEPARTMENTTREE_FAILURE.getDesc());
		}
		return result;
	}
}
